package org.lib.webvalidation.controller;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

import org.lib.webvalidation.dto.Name2DTO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * web2系列接口
 * @author ben
 * @date 2021-09-26 11:10:09 CST
 */
@RestController
@RequestMapping(value="/web2")
@Validated
public class Web2Controller {

	/**
	 * 类上的@Validated + Get请求参数加注解校验
	 * @author ben
	 * @date 2021-09-26 11:42:42 CST
	 * @param name
	 * @return
	 */
	@GetMapping(value="/hello")
	public String hello(@RequestParam @NotBlank(message="name不能为空") @Size(max=100, message="name长度超过100") String name) {
		return "Hello, " + name;
	}
	
	/**
	 * 2参数校验
	 * @author ben
	 * @date 2021-09-26 11:50:42 CST
	 * @param name
	 * @param age
	 * @return
	 */
	@GetMapping(value="/hello2")
	public String hello2(@RequestParam @NotBlank(message="name不能为空") @Size(max=100, message="name长度超过100") String name,
			@RequestParam @Min(value=0, message="age必须大于等于0") @Max(value=150, message="age必须小于等于150") Integer age) {
		return "Hello, " + name + ", you are " + age;
	}
	
	/**
	 * 多参数放入DTO
	 * @author ben
	 * @date 2021-09-26 11:58:46 CST
	 * @param dto
	 * @return
	 */
	@GetMapping(value="/hello3")
	// 1、未做校验
//	public String hello3(Name2DTO dto) {
	// 2、@Validated 做了校验，返回400
	public String hello3(@Validated Name2DTO dto) {
	// 3、@Valid 做了校验，返回400
//	public String hello3(@Valid Name2DTO dto) {
		return "Hello, " + dto.getName() + ", you are " + dto.getAge();
	}
	
}
